package club.ljwang.service;

import club.ljwang.dao.RoleMapper;
import club.ljwang.entity.QueryPageBean;
import club.ljwang.entity.Result;
import club.ljwang.exception.RoleException;
import club.ljwang.pojo.Menu;
import club.ljwang.pojo.Permission;
import club.ljwang.pojo.Role;
import club.ljwang.pojo.RoleList;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Permission> findAllPermission() {
        return roleMapper.findAllPermission();
    }

    @Override
    public List<Menu> findAllMenu() {
        Map map = new HashMap();
        Integer id = 0;
        List<Menu> menuList = roleMapper.findAllMenu(id);
        //递归查询菜单数据
        getChildren(menuList);

        return menuList;
    }

    @Override
    public List<Role> findPage(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Role> roles = roleMapper.findPage(queryPageBean.getQueryString());
        return roles;
    }

    @Override
    public void add(Role role) {
        roleMapper.add(role);
        Integer Roleid = role.getId();
        List<Integer> permissionIds = role.getPermissionIds();
        if (CollectionUtil.isNotEmpty(permissionIds)) {
            for (Integer permissionId : permissionIds) {
                roleMapper.setRoleAndPermission(permissionId,Roleid);
            }
        }
        List<Integer> menuIds = role.getMenuIds();
        if (CollectionUtil.isNotEmpty(menuIds)) {
            for (Integer menuId : menuIds) {
                roleMapper.setRoleAndMenu(menuId,Roleid);
            }
        }
    }

    @Override
    public void delete(Integer id) {
        Integer permissionCount = roleMapper.findRoleByPermissionId(id);
        Integer menuCount = roleMapper.findRoleByMenuId(id);
        if (menuCount > 0 || permissionCount > 0) {
            throw new RoleException("该角色存在引用,无法删除");
        } else {
            //删除权限中间表
            roleMapper.deletePermissionIds(id);
            //删除菜单表
            roleMapper.deleteMenuIds(id);
            //删除角色
            roleMapper.deleteRole(id);
        }

    }

    @Override
    public RoleList findByIdForEdit(Integer id) {
        RoleList roleList = new RoleList();
        //角色信息
        Role role = roleMapper.findById(id);
        //所有的权限信息
        List<Permission> permissions = roleMapper.findAllPermission();
        //用户勾选的权限id集合
        List<Integer> permissionIds = roleMapper.findPermissionIdsByRoleId(id);
        //所有的菜单信息
        List<Menu> menus = findAllMenu();
        //用户勾选的菜单id集合
        List<Integer> menuIds = roleMapper.findMenuIdsByRoleId(id);
        roleList.setRole(role);
        roleList.setPermissions(permissions);
        roleList.setPermissionIds(permissionIds);
        roleList.setMenus(menus);
        roleList.setMenuIds(menuIds);
        return roleList;
    }

    @Override
    public void edit(Role role) {
        //根据id修改角色的基本信息
        roleMapper.edit(role);
        //删除原来的关系
        roleMapper.deleteRoleAndPermission(role.getId());
        //建立新的关系
        List<Integer> permissionIds = role.getPermissionIds();
        if (CollectionUtil.isNotEmpty(permissionIds)) {
            for (Integer permissionId : permissionIds) {
                roleMapper.setRoleAndPermission(permissionId,role.getId());
            }
        }
        roleMapper.deleteRoleAndMenu(role.getId());
        List<Integer> menuIds = role.getMenuIds();
        if (CollectionUtil.isNotEmpty(menuIds)) {
            for (Integer menuId : menuIds) {
                roleMapper.setRoleAndMenu(menuId,role.getId());
            }
        }
    }

    @Override
    public List<Integer> getMenus(Integer roleId) {
        return roleMapper.getMenus(roleId);
    }

    //递归查询菜单数据
    private void getChildren(List<Menu> menuList){
        if (CollectionUtil.isNotEmpty(menuList)) {
            for (Menu menu : menuList) {
                Integer id = menu.getId();
                List<Menu> menus = roleMapper.findAllMenu(id);
                menu.setChildren(menus);
                getChildren(menus);
            }
        }
    }
}
